﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace mqtt_app
{
    public partial class Form5 : Form
    {
        public Form5()
        {
            InitializeComponent();
        }
        
        /// <summary>
        /// DataTable  生成XML文件
        /// </summary>
        /// <param name="dtTable">DataTable _dtImg = new DataTable("imgdata"); DataRow newRow = this._dtImg.NewRow();newRow["LocalPath"] = "名字";</param>
        /// <param name="strXMLName">XML名称  例如：  + "tmpimgName.xml"</param>
        /// <param name="strSaveXMLPath">保存的文件夹地址 例如： "Image\\unfinished\\"</param>
        /// <returns></returns>
        public static bool DataTableToXMLPath(DataTable dtTable, string strXMLName, string strSaveXMLPath)
        {
            MemoryStream stream = null;
            XmlTextWriter writer = null;
            StreamWriter sw = null;
            string pathname = AppDomain.CurrentDomain.BaseDirectory + strSaveXMLPath;
            try
            {
                if (!Directory.Exists(pathname)) Directory.CreateDirectory(pathname);
                stream = new MemoryStream();
                writer = new XmlTextWriter(stream, Encoding.UTF8);
                dtTable.WriteXml(writer, XmlWriteMode.WriteSchema);
                int nCount = (int)stream.Length;
                byte[] arr = new byte[nCount];
                stream.Seek(0, SeekOrigin.Begin);
                stream.Read(arr, 0, nCount);
                UTF8Encoding utf = new UTF8Encoding();
                string strContent = utf.GetString(arr).Trim();
                sw = new StreamWriter(pathname + strXMLName);
                sw.Write(strContent);
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                if (writer != null)
                {
                    writer.Close();
                }
                if (sw != null)
                {
                    sw.Close();
                }
            }
        }
        /// <summary>
        ///  根据指定的地址获取xml文件进行转换 文件地址
        /// </summary>
        /// <param name="xmlPath"></param>
        /// <returns></returns>
        public DataTable ConvertXMLToDataDataTable(string xmlPath)
        {
            StringReader stream = null;
            XmlTextReader reader = null;
            try
            {
                XmlDocument xmldoc = new XmlDocument();
                //根据地址加载Xml文件  
                xmldoc.Load(xmlPath);
                DataTable dt = new DataTable();
                stream = new StringReader(xmldoc.InnerXml);
                reader = new XmlTextReader(stream);
                dt.ReadXml(reader);
                return dt;
            }
            catch
            {
                return null;
            }
            finally
            {
                if (reader != null)
                    reader.Close();
                if (stream != null)
                    stream.Close();
            }
        }
        private void Form5_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            

                DataTable _dtImg = new DataTable("设备类型");
                _dtImg.Columns.Add("类型ID");
                _dtImg.Columns.Add("类型");
                
                DataRow newRow = _dtImg.NewRow();
                newRow["类型ID"] = 0x01;
                newRow["类型"] = "定位手环2G V0.1";                
                _dtImg.Rows.Add(newRow);

                //newRow["类型ID"] = 0x02;
                //newRow["类型"] = "海上腕表";
                //_dtImg.Rows.Add(newRow);

            DataTableToXMLPath(_dtImg, "main.xml", "");
                //string pathname = AppDomain.CurrentDomain.BaseDirectory + "main.xml";
                //DataTable dataTable = ConvertXMLToDataDataTable(pathname);
                //foreach (DataRow item in dataTable.Rows)
                //{
                //    var ss = item["列名1"];
                //}
                //var str = dataTable.Rows[0]["列名1"];

            
        }
        DataTable dt1;
        DataTable dt_attr;
        private void button2_Click(object sender, EventArgs e)
        {


            string pathname;
            pathname = AppDomain.CurrentDomain.BaseDirectory + "main.xml";
            dt1 = ConvertXMLToDataDataTable(pathname);
            comboBox1.DataSource = dt1;
            comboBox1.DisplayMember = "类型";
            comboBox1.ValueMember = "类型ID";
            
            pathname = AppDomain.CurrentDomain.BaseDirectory + "attribute.xml";
            dt_attr = ConvertXMLToDataDataTable(pathname);
            comboBox2.DataSource = dt_attr;
            comboBox2.DisplayMember = "说明";
            comboBox2.ValueMember = "属性ID";
            
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int iCurrentIndex = this.comboBox1.SelectedIndex;
            if (iCurrentIndex < 0) return;

            DataRow dr = dt1.Rows[iCurrentIndex];
            Console.WriteLine( dr["类型ID"].ToString());
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string unicodeString = "张三";
            UTF8Encoding utf8 = new UTF8Encoding();
            Byte[] encodedBytes = utf8.GetBytes(unicodeString);
            Console.WriteLine(encodedBytes.ToString());
            
        }
    }
}
